<template>
	<div>
		<uni-search-bar v-model="sv" placeholder="搜索" @confirm="doSearch"></uni-search-bar>
		<uni-list>				
			<uni-list-item v-for="(s, i) in list" :title="s.num"
				clickable :rightText="dictLabel(statusOptions,s.status)" :note="stockDetail(s)"
				@click="showDetail(s)" showArrow></uni-list-item>
		</uni-list>
		<uni-load-more :status="more" @clickLoadMore="loadMore" v-if="more == 'more'"></uni-load-more>
	</div>
</template>

<script>
	import { apiGet, apiDs	} from '@/api/openapi.js'
	import {getDicts, selectDictLabel	} from '@/api/sys.js'
	export default {
		data() {
			return {
				stockId: null,
				list: [],
				total: 0,
				more: 'noMore',
				loaded: 0,
				statusOptions:[],
				sv:null,
				pager:{
					page:1, 
					limit:10,
				}
			}
		},
		onLoad(option) {
			this.stockId = option.id;
			this.loadMore();
			
			getDicts('assets_status').then(response => {
				response.data.forEach(e=>{
					e.value = e.dictValue;
					e.text = e.dictLabel;
				})
				this.statusOptions = response.data;	
			});
		},
		onShow() {	
			this.list = [];
			this.doSearch();
		},
		methods: {
			loadMore() {
				this.more = 'loading';
				let params = {
					"asset_stock_detail":{"@alias":"t","stock_id":this.stockId,"num":null,"@exp":{"num":"lk"}},
					"asset_warehouse":{"@alias":"h","@join":"t.house_id = h.id", "@column":"name:house_name"},
					"sys_user":{"@alias":"u","@join":"LEFT t.user_id = u.user_id", "@column":"nick_name:user_name"},
					"@pager":{... this.pager}
				};
				if(this.sv){
					params['asset_stock_detail']['num'] = this.sv
				};
				apiGet(params).then(response => {
					this.list = this.list.concat(response.data.rows);
					this.loaded += response.data.rows.length;
					if (response.data.rows.length >= this.pager.limit) {
						this.more = 'more';
					} else {
						this.more = 'noMore';
					}
				});
			},
			doSearch() {
				this.loaded = 0;
				this.pager.page = 1;
				this.loadMore();
			},
			cancelSerach() {
				this.doSearch();
				this.sv = null;
			},
			dictLabel(dictData, dictVal) {
				return selectDictLabel(dictData, dictVal);
			},
			stockDetail(s){
				let d = '型号：'+s.cate_name + ' ' + s.model_name + ', 在 ' + s.house_name ;
				if(s.price){
					d += ',价值：'+s.price;
				}				
				if(s.user_name){
					d += ',当前使用人:'+s.user_name;
				}				
				return d;
			},
			showDetail(s) {
				uni.navigateTo({
					url: '/pages/asset/stock/asset?action=view&id='+s.id
				});
			}
		}
	}
</script>

<style>

</style>
